/**
 * @class   MediaDirectorParser
 * @brief   Builds the wosh::entertainment::MediaZone collection (tree/list)
 *          and keeps information up to date.
 *
 * 
 *
 * MediaZone definition:
 *
 *  - User-const-defined <alias, settings, info> by configuration file (database)
 *
 *  - User-dynamic-defined <alias, settings, info> by configuration file (database),
 *    but they are keep up to date (and never deleted)
 *
 *  - dynamic : created and fully managed by MediaDirector, hierarchical, transient,
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: MediaDirectorParser.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_MediaDirectorParser_H__
 #define __WOSH_Services_MediaDirectorParser_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Containters.h>
 #include <core/Table.h>
 #include <core/Notification.h>
 #include <core/InterfacesProvider.h>
 #include <core/MessageQueue.h>

 #include <core/Communicator.h>
 #include <core/Interpreter.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/InterpreterService.h>
 #include <interfaces/services/BuildingManager.h>

 #include <framework/entertainment/MediaZone.h>
 #include <framework/entertainment/MediaTrack.h>

 #include "IMediaDirectorListener.h"

 #include <interfaces/devices/PlayerAudio.h>
 #include <framework/building/AudioBox.h>
 #include <framework/building/Room.h>

using namespace wosh;
using namespace wosh::building;
using namespace wosh::entertainment;


class MediaDirectorParser : public BundleGenericWorker  {

	public:

		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		MediaDirectorParser( BundleGeneric& bundle,
							MapT<string, MediaZone*>& zones,
							BusConnector& busDevices, BusConnector& busBuilding );

		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~MediaDirectorParser();





void updateAll( const string& kernel );


MediaZone* createZoneByDeviceUri_( const URI& uri_of_device );

MediaZone* getZoneByDeviceUri_( const URI& device_uri );

void requestMultimediaDevices( const string& kernel );
void evalResponseDeviceProperties( const wosh::Table* props_of_device, const URI& uri_of_device );

void requestAudioBoxesProperties( const string& kernel );
void evalAudioBoxesProperties( const wosh::Table* grouped_props, const URI& bobj_uri );


void requestAudioBoxProperties( const string& kernel, const URI& device_uri );
void evalAudioBoxProperties( const wosh::Table* props, const URI& bobj_uri );




void requestRoomChildren( const string& kernel );
void evalRoomChildren( const wosh::Table* children_of_room, const URI& room_uri );






void requestBuildingObjects( const string& kernel );



	public:

		void evalMessageIncoming( const Message& message );

	//	MediaZone* getPlayerOfLocation_( const string& locationUri );
		MediaZone* getPlayerByBuildingObj_( const string& bobjUri );

		MediaZone* getNearestMediaZone_( MediaZone* zone, bool playing_flag );

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		void setListener( IMediaDirectorListener* list )	{ this->listener = list; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:

		MapT<string, MediaZone*>& getZones()					{ return this->zones; }
		const MapT<string, MediaZone*>& getZones() const		{ return this->zones; }


//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		void evalMessage( Message* message );

		void evalPlayerBuildingChildren_( const wosh::Table* childrenprops, const URI& uri );
		void evalPlayerBuilding_( const wosh::Table* childrenprops, const URI& uri );
		void evalPlayerDevice_( const wosh::Table* props, const URI& uri );

		void evalTimeOut();

	protected:
		/**
		 * @brief   
		 * @see     running
		 * @see     
		 */
		virtual void runThread();

//@}
	protected:

		MapT<string, MediaZone*>& zones;	///< name, data
		int timeoutEval;

		BusConnector& BusDevices;
		BusConnector& BusBuilding;


		IMediaDirectorListener* listener;	///< callbacks


}; // class def


#endif //__WOSH_Services_MediaDirectorParser_H__
